from django.shortcuts import render
# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView
from users.models import User
import re


class RegisterView(APIView):
    def get(self,request):
        pass
    def post(self,request):
        # 接收参数
        username = request.data.get('username','')
        if username == '':
            return Response({'code':1001,'msg':'用户名不能为空'})
        is_username = re.compile(u'[\s]')
        if ' ' in is_username.findall(username):
            return Response({'code':1002,'msg':'用户名中不能携带空格'})
        password = request.data.get('password','')
        if len(password) < 6:
            return Response({'code':1003,'msg':'密码长度需大于6'})
        phone = request.data.get('phone','')
        is_phone = re.compile("^1[358]\d{9}$|^147\d{8}$|^179\d{8}$")
        if not is_phone.match(phone):
            return Response({'code': 1004, 'msg': '手机号格式有误'})
        email = request.data.get('email','')
        if '@' not in email:
            return Response({'code':1005,'msg':'邮箱格式有误'})
        img = request.data.get('img','')
        brief = request.data.get('brief','')
        address = request.data.get('address','')
        user = User.objects.first(username=username).filter()
        if user:
            return Response({'code':403,'msg':'用户名已存在'})

    def put(self,request):
        pass
    def delete(self,request):
        pass